import hashlib
import struct
import sys


# 调用方式:
# python3 sitem_shard.py [pItemId]
# example:
# python3 sitem_shard.py 12
def get_shard_index(p_item_id):
    num_shards = 128  # 128个分表
    hasher = hashlib.md5()
    hasher.update(struct.pack('>q', p_item_id))  # 使用大端字节序
    hash_bytes = hasher.digest()
    shard_index = int.from_bytes(hash_bytes[:4], byteorder='big') % num_shards
    return shard_index


if __name__ == "__main__":
    # if len(sys.argv) != 2:
    #     print("Usage: python script.py <pitemId>")
    #     sys.exit(1)
    #
    # p_item_id = int(sys.argv[1])
    shard_index = get_shard_index(4157)
    print("分表shard:", shard_index)

